

//
//  Post.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/21/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit


struct RuleType {
    var value: Int
    var display: String
}

struct PostImage: Decodable, Equatable {
    var image: String
    var image_thumbnail: String
    var id: Int
}

enum ADStatus: Int {
    case active = 1
    case created = 0
    case archived = 2
}

struct Post: Decodable {
    var id: Int
    var visCount: Int
    var car: Car
    var city: City?
    var price: Int
    var ruleType: RuleType?
    var timestamp: String? = nil
    var description: String? = nil
    var images: [PostImage]? = []
    var contactName: String? = nil
    var contactEmail: String? = nil
    var contactNumbers: [NumberViewModel.Number]? = []
    var status: ADStatus = .archived
    
    
    var isLiked: Bool = false
    var isNew: Bool = false
    var isRassrochka: Bool = false
    var isSrochno: Bool = false
    var isRastomozhen: Bool = false
    var isObmen: Bool = false
    
    // LOCAL DATA
    var localImgs: [UIImage]? = nil
    var customOptions: [String:Any]? = nil
    var saveToGarage: Bool = false
    
    // MARK: Tarif
    var isTop = false
    var isColored  = false
    var isSpecial  = false
    var isSearchUp = false
    
    enum CodingKeys: CodingKey {
        case car, views_count, price, city
        case created_at, description, id, contact_name, contact_email
        case images, in_fav, contact_phones, cleared_by_customs, exchange, status
    }
    
    init(from decoder: Decoder) throws {
        let values = try decoder.container(keyedBy: CodingKeys.self)
        self.id = try values.decode(Int.self, forKey: .id)
        self.car = try values.decode(Car.self, forKey: .car)
        self.visCount = try values.decode(Int.self, forKey: .views_count)
        self.price = Int(Double(try values.decode(String.self, forKey: .price)) ?? 0.0)
        self.city = try? values.decode(City.self, forKey: .city)
        self.timestamp = try? values.decode(String.self, forKey: .created_at)
        self.description = try? values.decode(String.self, forKey: .description)
        self.contactName = try? values.decode(String.self, forKey: .contact_name)
        self.contactEmail = try? values.decode(String.self, forKey: .contact_email)
        self.contactNumbers = try? values.decode([NumberViewModel.Number].self, forKey: .contact_phones)
        self.images = try? values.decode([PostImage].self, forKey: .images)
        self.isLiked = (try? values.decode(Bool.self, forKey: .in_fav)) ?? false
        self.isRastomozhen = (try? values.decode(Bool.self, forKey: .cleared_by_customs)) ?? false
        self.isObmen = (try? values.decode(Bool.self, forKey: .exchange)) ?? false
        self.status = ADStatus(rawValue: (try? values.decode(Int.self, forKey: .status))!) ?? ADStatus.archived
    }
    
    
    init() {
        self.car = Car(id: 0, carNumber: CarNumber(isNewFormat: true, number: 232, letters: "AAA", cityId: "22", vin: "2323"))
        self.car.marka = Marka(id: 0, ext: 1, name_ru: "Mercedes", logo: nil)
        self.car.model = Model(id: 2, ext: 2, name_ru: "Benz", generation: nil)
        self.images = [PostImage(image: "https://i.ibb.co/b2VDxm8/image-26.png", image_thumbnail: "https://i.ibb.co/b2VDxm8/image-26.png", id: 1)]
        
        self.visCount = 203232
        self.price = 23232323
        self.city = City(id: 1, name: "Moskva", region: City.Region(id: 1, name: "Astana", country: 3))
        self.id = 2        
    }
}
